home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / dskut / patch12.zip / INP.C < prev    next >
C/C++ Source or Header  |  1990-05-30  |  8KB  |  320 lines

  1. /* $Header: inp.c,v 2.0.1.1 88/06/03 15:06:13 lwall Locked $
  2.  *
  3.  * $Log:    inp.c,v $
  4.  * Revision 2.0.1.1  88/06/03  15:06:13  lwall
  5.  * patch10: made a little smarter about sccs files
  6.  * 
  7.  * Revision 2.0  86/09/17  15:37:02  lwall
  8.  * Baseline for netwide release.
  9.  * 
  10.  */
  11.  
  12. #include "EXTERN.h"
  13. #include "common.h"
  14. #include "util.h"
  15. #include "pch.h"
  16. #include "INTERN.h"
  17. #include "inp.h"
  18.  
  19. /* Input-file-with-indexable-lines abstract type */
  20.  
  21. static long i_size;            /* size of the input file */
  22. static char *i_womp;            /* plan a buffer for entire file */
  23. static char **i_ptr;            /* pointers to lines in i_womp */
  24.  
  25. static int tifd = -1;            /* plan b virtual string array */
  26. static char *tibuf[2];            /* plan b buffers */
  27. static LINENUM tiline[2] = {-1, -1};    /* 1st line in each buffer */
  28. static LINENUM lines_per_buf;        /* how many lines per buffer */
  29. static int tireclen;            /* length of records in tmp file */
  30.  
  31. /* New patch--prepare to edit another file. */
  32.  
  33. void
  34. re_input()
  35. {
  36.     if (using_plan_a) {
  37.     i_size = 0;
  38. #ifndef lint
  39.     if (i_ptr != Null(char**))
  40.         free((char *)i_ptr);
  41. #endif
  42.     if (i_womp != Nullch)
  43.         free(i_womp);
  44.     i_womp = Nullch;
  45.     i_ptr = Null(char **);
  46.     }
  47.     else {
  48.     using_plan_a = TRUE;        /* maybe the next one is smaller */
  49.     Close(tifd);
  50.     tifd = -1;
  51.     free(tibuf[0]);
  52.     free(tibuf[1]);
  53.     tibuf[0] = tibuf[1] = Nullch;
  54.     tiline[0] = tiline[1] = -1;
  55.     tireclen = 0;
  56.     }
  57. }
  58.  
  59. /* Constuct the line index, somehow or other. */
  60.  
  61. void
  62. scan_input(filename)
  63. char *filename;
  64. {
  65.     if (!plan_a(filename))
  66.     plan_b(filename);
  67.     if (verbose) {
  68.     say3("Patching file %s using Plan %s...\n", filename,
  69.       (using_plan_a ? "A" : "B") );
  70.     }
  71. }
  72.  
  73. /* Try keeping everything in memory. */
  74.  
  75. bool
  76. plan_a(filename)
  77. char *filename;
  78. {
  79.     int ifd;
  80.     Reg1 char *s;
  81.     Reg2 LINENUM iline;
  82.  
  83.     if (ok_to_create_file && stat(filename, &filestat) < 0) {
  84.     if (verbose)
  85.         say2("(Creating file %s...)\n",filename);
  86.     makedirs(filename, TRUE);
  87.     close(creat(filename, 0666));
  88.     }
  89.     if (stat(filename, &filestat) < 0) {
  90.     Sprintf(buf, "RCS/%s%s", filename, RCSSUFFIX);
  91.     if (stat(buf, &filestat) >= 0 || stat(buf+4, &filestat) >= 0) {
  92.         Sprintf(buf, CHECKOUT, filename);
  93.         if (verbose)
  94.         say2("Can't find %s--attempting to check it out from RCS.\n",
  95.             filename);
  96.         if (system(buf) || stat(filename, &filestat))
  97.         fatal2("Can't check out %s.\n", filename);
  98.     }
  99.     else {
  100.         Sprintf(buf+20, "SCCS/%s%s", SCCSPREFIX, filename);
  101.         if (stat(s=buf+20, &filestat) >= 0 ||
  102.           stat(s=buf+25, &filestat) >= 0) {
  103.         Sprintf(buf, GET, s);
  104.         if (verbose)
  105.             say2("Can't find %s--attempting to get it from SCCS.\n",
  106.             filename);
  107.         if (system(buf) || stat(filename, &filestat))
  108.             fatal2("Can't get %s.\n", filename);
  109.         }
  110.         else
  111.         fatal2("Can't find %s.\n", filename);
  112.     }
  113.     }
  114.     filemode = filestat.st_mode;
  115.     if ((filemode & S_IFMT) & ~S_IFREG)
  116.     fatal2("%s is not a normal file--can't patch.\n", filename);
  117.     i_size = filestat.st_size;
  118.     if (out_of_mem) {
  119.     set_hunkmax();        /* make sure dynamic arrays are allocated */
  120.     out_of_mem = FALSE;
  121.     return FALSE;            /* force plan b because plan a bombed */
  122.     }
  123. #ifdef lint
  124.     i_womp = Nullch;
  125. #else
  126.     i_womp = malloc((MEM)(i_size+2));    /* lint says this may alloc less than */
  127.                     /* i_size, but that's okay, I think. */
  128. #endif
  129.     if (i_womp == Nullch)
  130.     return FALSE;
  131.     if ((ifd = open(filename, 0)) < 0)
  132.     fatal2("Can't open file %s\n", filename);
  133. #ifndef lint
  134.     if (read(ifd, i_womp, (int)i_size) != i_size) {
  135.     Close(ifd);    /* probably means i_size > 15 or 16 bits worth */
  136.     free(i_womp);    /* at this point it doesn't matter if i_womp was */
  137.     return FALSE;    /*   undersized. */
  138.     }
  139. #endif
  140.     Close(ifd);
  141.     if (i_size && i_womp[i_size-1] != '\n')
  142.     i_womp[i_size++] = '\n';
  143.     i_womp[i_size] = '\0';
  144.  
  145.     /* count the lines in the buffer so we know how many pointers we need */
  146.  
  147.     iline = 0;
  148.     for (s=i_womp; *s; s++) {
  149.     if (*s == '\n')
  150.         iline++;
  151.     }
  152. #ifdef lint
  153.     i_ptr = Null(char**);
  154. #else
  155.     i_ptr = (char **)malloc((MEM)((iline + 2) * sizeof(char *)));
  156. #endif
  157.     if (i_ptr == Null(char **)) {    /* shucks, it was a near thing */
  158.     free((char *)i_womp);
  159.     return FALSE;
  160.     }
  161.     
  162.     /* now scan the buffer and build pointer array */
  163.  
  164.     iline = 1;
  165.     i_ptr[iline] = i_womp;
  166.     for (s=i_womp; *s; s++) {
  167.     if (*s == '\n')
  168.         i_ptr[++iline] = s+1;    /* these are NOT null terminated */
  169.     }
  170.     input_lines = iline - 1;
  171.  
  172.     /* now check for revision, if any */
  173.  
  174.     if (revision != Nullch) { 
  175.     if (!rev_in_string(i_womp)) {
  176.         if (force) {
  177.         if (verbose)
  178.             say2(
  179. "Warning: this file doesn't appear to be the %s version--patching anyway.\n",
  180.             revision);
  181.         }
  182.         else {
  183.         ask2(
  184. "This file doesn't appear to be the %s version--patch anyway? [n] ",
  185.             revision);
  186.         if (*buf != 'y')
  187.         fatal1("Aborted.\n");
  188.         }
  189.     }
  190.     else if (verbose)
  191.         say2("Good.  This file appears to be the %s version.\n",
  192.         revision);
  193.     }
  194.     return TRUE;            /* plan a will work */
  195. }
  196.  
  197. /* Keep (virtually) nothing in memory. */
  198.  
  199. void
  200. plan_b(filename)
  201. char *filename;
  202. {
  203.     Reg3 FILE *ifp;
  204.     Reg1 int i = 0;
  205.     Reg2 int maxlen = 1;
  206.     Reg4 bool found_revision = (revision == Nullch);
  207.  
  208.     using_plan_a = FALSE;
  209.     if ((ifp = fopen(filename, "r")) == Nullfp)
  210.     fatal2("Can't open file %s\n", filename);
  211.     if ((tifd = creat(TMPINNAME, 0666)) < 0)
  212.     fatal2("Can't open file %s\n", TMPINNAME);
  213.     while (fgets(buf, sizeof buf, ifp) != Nullch) {
  214.     if (revision != Nullch && !found_revision && rev_in_string(buf))
  215.         found_revision = TRUE;
  216.     if ((i = strlen(buf)) > maxlen)
  217.         maxlen = i;            /* find longest line */
  218.     }
  219.     if (revision != Nullch) {
  220.     if (!found_revision) {
  221.         if (force) {
  222.         if (verbose)
  223.             say2(
  224. "Warning: this file doesn't appear to be the %s version--patching anyway.\n",
  225.             revision);
  226.         }
  227.         else {
  228.         ask2(
  229. "This file doesn't appear to be the %s version--patch anyway? [n] ",
  230.             revision);
  231.         if (*buf != 'y')
  232.             fatal1("Aborted.\n");
  233.         }
  234.     }
  235.     else if (verbose)
  236.         say2("Good.  This file appears to be the %s version.\n",
  237.         revision);
  238.     }
  239.     Fseek(ifp, 0L, 0);        /* rewind file */
  240.     lines_per_buf = BUFFERSIZE / maxlen;
  241.     tireclen = maxlen;
  242.     tibuf[0] = malloc((MEM)(BUFFERSIZE + 1));
  243.     tibuf[1] = malloc((MEM)(BUFFERSIZE + 1));
  244.     if (tibuf[1] == Nullch)
  245.     fatal1("Can't seem to get enough memory.\n");
  246.     for (i=1; ; i++) {
  247.     if (! (i % lines_per_buf))    /* new block */
  248.         if (write(tifd, tibuf[0], BUFFERSIZE) < BUFFERSIZE)
  249.         fatal1("patch: can't write temp file.\n");
  250.     if (fgets(tibuf[0] + maxlen * (i%lines_per_buf), maxlen + 1, ifp)
  251.       == Nullch) {
  252.         input_lines = i - 1;
  253.         if (i % lines_per_buf)
  254.         if (write(tifd, tibuf[0], BUFFERSIZE) < BUFFERSIZE)
  255.             fatal1("patch: can't write temp file.\n");
  256.         break;
  257.     }
  258.     }
  259.     Fclose(ifp);
  260.     Close(tifd);
  261.     if ((tifd = open(TMPINNAME, 0)) < 0) {
  262.     fatal2("Can't reopen file %s\n", TMPINNAME);
  263.     }
  264. }
  265.  
  266. /* Fetch a line from the input file, \n terminated, not necessarily \0. */
  267.  
  268. char *
  269. ifetch(line,whichbuf)
  270. Reg1 LINENUM line;
  271. int whichbuf;                /* ignored when file in memory */
  272. {
  273.     if (line < 1 || line > input_lines)
  274.     return "";
  275.     if (using_plan_a)
  276.     return i_ptr[line];
  277.     else {
  278.     LINENUM offline = line % lines_per_buf;
  279.     LINENUM baseline = line - offline;
  280.  
  281.     if (tiline[0] == baseline)
  282.         whichbuf = 0;
  283.     else if (tiline[1] == baseline)
  284.         whichbuf = 1;
  285.     else {
  286.         tiline[whichbuf] = baseline;
  287. #ifndef lint        /* complains of long accuracy */
  288.         Lseek(tifd, (long)baseline / lines_per_buf * BUFFERSIZE, 0);
  289. #endif
  290.         if (read(tifd, tibuf[whichbuf], BUFFERSIZE) < 0)
  291.         fatal2("Error reading tmp file %s.\n", TMPINNAME);
  292.     }
  293.     return tibuf[whichbuf] + (tireclen*offline);
  294.     }
  295. }
  296.  
  297. /* True if the string argument contains the revision number we want. */
  298.  
  299. bool
  300. rev_in_string(string)
  301. char *string;
  302. {
  303.     Reg1 char *s;
  304.     Reg2 int patlen;
  305.  
  306.     if (revision == Nullch)
  307.     return TRUE;
  308.     patlen = strlen